package conf

import (
	"fmt"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

const DbUsername = "postgres"
const DbPassword = "sde"
const DbName = "urbanxyz_ningbo"
const DbHost = "192.168.255.210"
const DbPort = "5436"

var Db *gorm.DB

func InitDb() *gorm.DB {
	Db = connectDB()
	return Db
}

func connectDB() *gorm.DB {
	var err error

	dsn := "host=" + DbHost + " user=" + DbUsername + " password=" + DbPassword + " dbname=" + DbName + " port=" + DbPort + " sslmode=disable TimeZone=Asia/Shanghai"

	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

	if err != nil {
		fmt.Printf("Error connecting to database : error=%v\n", err)
		return nil
	}

	return db
}
